/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.article;

import java.util.ArrayList;
import java.util.Collection;
import model.generic.TableModelWithListeners;
import util.article.ModelDetails;

/**
 *
 * @author Dirk
 */
public class ModelTableModel extends TableModelWithListeners {
    
    private final String[] columns = {"Model code", "Model naam"};
    private ArrayList<ModelDetails> models;
    
    
    public ModelTableModel() {
        models = new ArrayList<ModelDetails>();
    }
    
    
    
    @Override
    public int getRowCount() {
        return models.size() + 1;
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(rowIndex < models.size()){
            if(columnIndex == 0)
                return models.get(rowIndex).getCode();
            else
                return models.get(rowIndex).getName();
        } else
            return "";
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if(rowIndex >= models.size()){
            ModelDetails size = new ModelDetails(-1, "", "");
            models.add(size);}
        
        if(((String)aValue).equals(""))
            models.remove(rowIndex);
        else if(columnIndex == 0)
            models.get(rowIndex).setCode((String)aValue);
        else
            models.get(rowIndex).setName((String)aValue);
        this.fireTableDataChanged();
    }

    @Override
    public void removeItem(int index) {
        models.remove(index);
        this.fireTableDataChanged();
    }
    
    public Collection<ModelDetails> getAllModels(){
        return models;
    }
    
    
    
    
    
}
